﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WeChat.PlatForm.Entity;
using Microsoft.EntityFrameworkCore;
using WeChat.PlatForm.IService;
using Microsoft.AspNetCore.Http;
using WeChat.PlatForm.Web.Models;
using Microsoft.AspNetCore.Authorization;
using WeChat.PlatForm.DTO;
using WeChat.PlatForm.Common;
using Microsoft.AspNetCore.Hosting;

namespace WeChat.PlatForm.Web.Controllers
{
    public class HomeController : Controller
    {
        IMemberService _member;
        ITestService _test;

        //内置服务，.net core内置服务
        IHostingEnvironment _environment;

        public HomeController(IMemberService member,
            IHostingEnvironment environment, ITestService test)
        {
            _test = test;
            _member = member;
            _environment = environment;
        }

        /// <summary>
        /// 展示登录界面
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            //手动得到服务
            //IMemberService members =(IMemberService)HttpContext.RequestServices.GetService(typeof(IMemberService));
            //RedisHelp.SetRedis("good","你好");
            //string str = RedisHelp.GetRedis("good");
            
            HttpContext.Session.SetString("key", "redissession成功");
            //得到运行的项目根目录的绝对路径
            string one = _environment.ContentRootPath;
            //得到静态文件根目录的绝对路径
            string web = _environment.WebRootPath;

            //_test.ToTest();

            return View();
        }

        /// <summary>
        /// 去登陆
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ToLogin(string email, string password)
        {
            JsonMsg msg = new JsonMsg();

            //获取GET参数 Request.Query["Key"]
            //获取POST参数 Request.Form["Key"]
            //string email = Request.Query["email"];
            //string password = Request.Query["password"];

            if (string.IsNullOrEmpty(email))
            {
                msg.Status = 0;
                msg.Msg = "邮箱不能时空";
                return Json(msg);
            }

            if (string.IsNullOrEmpty(password))
            {
                msg.Status = 0;
                msg.Msg = "密码不能是空";
                return Json(msg);
            }

            int flg = await _member.ToLogin(email, password);

            if (flg == 0)
            {
                msg.Status = 0;
                msg.Msg = "用户名或者密码错误";
                return Json(msg);
            }

            msg.Status = 1;
            msg.Msg = "OK";
            return Json(msg);
        }

        /// <summary>
        /// 发送邮箱验证码
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> SendEmail()
        {
            try
            {
                JsonMsg msg = new JsonMsg();

                //获取GET参数 Request.Query["Key"]
                //获取POST参数 Request.Form["Key"]
                string email = Request.Form["email"];
                //string password = Request.Query["password"];

                if (string.IsNullOrEmpty(email))
                {
                    msg.Status = 0;
                    msg.Msg = "邮箱不能时空";
                    return Json(msg);
                }
                Random ran = new Random();

                int randnumber = ran.Next(100000, 999999);

                HttpContext.Session.SetString("email", randnumber.ToString());

                await CommonHelp.SmtpClient(email, "提示", "您的验证码(本应该发送一个连接来进行注册，但是我觉得知道就行，我就简单做了用验证码来验证，类似于手机的注册):" + randnumber);


                msg.Status = 1;
                msg.Msg = "OK";
                return Json(msg);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 注册会员
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> RegisterMember()
        {
            JsonMsg msg = new JsonMsg();

            string password = Request.Form["password"];
            string email = Request.Form["email"];
            string nick = Request.Form["nick"];
            string code = Request.Form["code"];

            string randnumber = HttpContext.Session.GetString("email");
            //取一次就要销毁
            HttpContext.Session.Remove("email");

            if (code != randnumber)
            {
                msg.Status = 0;
                msg.Msg = "验证码不正确";
                return Json(msg);
            }

            if (string.IsNullOrEmpty(email))
            {
                msg.Status = 0;
                msg.Msg = "邮箱不能时空";
                return Json(msg);
            }

            if (string.IsNullOrEmpty(password))
            {
                msg.Status = 0;
                msg.Msg = "密码不能是空";
                return Json(msg);
            }

            int flg = await _member.RegisterMember(password, email, nick);

            msg.Status = 1;
            msg.Msg = "OK";
            return Json(msg);
        }
    }
}
